// const { default: axios } = require("axios")

document.addEventListener('DOMContentLoaded', async () => {
    const token = localStorage.getItem('user-token')
    const { data: { data: res } } = await axios.get('/dashboard')
    console.log(res)
    // document.querySelector('[name="salary"]').innerHTML = res.overview.salary
    for (let key in res.overview) {
        document.querySelector(`[name="${key}"]`).innerHTML = res.overview[key]
    }

    // 调用图表的函数  折线图  全学科薪资走势
    initSalaryLineChart(res.year)

    // 调用图表的函数  饼状图  班级薪资分布
    initSalaryPieChart(res.salaryData)

    // 调用图表的函数  柱状图  班级每组薪资
    initSalaryBarChart(res.groupData)

    // 调用图表的函数  双饼状图  男女薪资分布
    initGenderSalaryPieChart(res.salaryData)

    // 调用图表的函数  地图 籍贯分布图
    initMapChart(res.provinceData)
})

// 折线图  全学科薪资走势
function initSalaryLineChart(year) {
    const myChart = echarts.init(document.querySelector('#line'))
    // let month = []
    // let salary = []
    // year.forEach(item => month.push(item.month))
    // year.forEach(item => salary.push(item.salary))
    const option = {
        title: {
            text: '2021全学科薪资走势',
            left: 15,
            top: 10,
            fontSize: 16
        },
        // 放在轴线上显示提示
        tooltip: {
            trigger: 'axis'
        },
        color:[
                {type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                    offset: 0, color: '#51adff' // 0% 处的颜色
                }, {
                    offset: 1, color: '#5f7af6' // 100% 处的颜色
                }],
            }
            ],
            // 调整图标位置
        grid: {
            top: '20%',
        },
        xAxis: {
            type: 'category',
            // 设置x轴的轴线样式
            axisLine: {
                lineStyle: {
                    color: '#999',
                    type: 'dashed'
                }
            },
            // 需要渲染的数据
            data: year.map(item => item.month)
        },
        yAxis: {
            type: 'value',
            // 设置y轴分隔线的样式
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        series: [
            {
                data: year.map(item => item.salary),
                type: 'line',
                // 线条平滑度
                smooth: true,
                // 线条上的修饰图形
                symbolSize: 10,
                // 数据渲染线的相关样式
                lineStyle: {
                    // color: rgb(102,102,102),
                    width: 6
                },
                // 数据下方的区域样式设置
                areaStyle:{
                    color:{type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                            offset: 0, color: '#9cd1ff' // 0% 处的颜色
                        },{
                            offset: 0.8, color: 'rgba(255,255,255,0.7)' // 80% 处的颜色
                        },{
                            offset: 1, color: 'rgba(255,255,255,0)'  // 100% 处的颜色
                        }],
                    }
                },
            }
        ]
    }
    myChart.setOption(option)
}

// 饼状图  班级薪资分布
function initSalaryPieChart(salaryData) {
    const myChart = echarts.init(document.querySelector('#salary'))

    // 方法一
    // const arr = []
    // salaryData.map(item => arr.push({ name: item.label, value: (item.g_count + item.b_count) },))
    // console.log(arr)
    const option = {
        title: {
            text: '班级薪资分布',
            top: 10,
            left: 15,
            fontSize: 16
        },
        legend: {
            bottom: '5%',
            left: 'center'
        },
        color:['#fda422','#5097ff','#3abcfa','#34d39a'],
        tooltip: {
            trigger: 'item'
        },
        series: [
            {
                name: '班级薪资分布',
                type: 'pie',
                radius: ['60%', '80%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 20,
                    borderColor: '#fff',
                    borderWidth: 2,
                    // color:['red','blue','yellow','green'],
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: false,
                        fontSize: '40',
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                // colorBy: ['#5470c6', '#91cc75', '#fac858', '#ee6666'],
                // data: arr

                // 方法二
                data: salaryData.map(item => {
                    return {
                        name: item.label,
                        value: item.g_count + item.b_count
                    }
                })
            }
        ]
    };
    myChart.setOption(option)
}

// 柱状图  班级每组薪资
function initSalaryBarChart(groupData) {
    const myChart = echarts.init(document.querySelector('#lines'))

    let arr = []
    // 方法一  事件委托的方式进行
    const btns = document.querySelector('#btns')
    // console.log(btns)
    btns.addEventListener('click', function (e) {
        // console.log(e.target.innerHTML)
        arr = []
        if (e.target.tagName === 'BUTTON') {
            if (document.querySelector('.btn-blue') !== null) {
                document.querySelector('button.btn-blue').classList.remove('btn-blue')
                e.target.classList.add('btn-blue')
            }
            arr = groupData[e.target.innerHTML].map(item => Object.values(item))
            render(arr)
        }
    })

    // 首屏渲染的数据
    arr = groupData[1].map(item => Object.values(item))

    //  封装渲染函数
    function render(arr) {
        const option = {
            legend: {
                show: false
            },
            tooltip: {},
            dataset: {
                source: [
                    ['product', '期望薪资', '就业薪资'],
                    ...arr
                ]
                // source: newArr
            },
            grid: {
                left: ' 8%',
                bottom: '15%'
            },
            xAxis: {
                type: 'category',
                axisLine: {
                    lineStyle: {
                        color: '#999',
                        type: 'dashed'
                    }
                },
            },

            yAxis: {
                splitLine: {
                    lineStyle: {
                        type: 'dashed'
                    }
                }
            },
            // Declare several bar series, each will be mapped
            // to a column of dataset.source by default.
            series: [{ type: 'bar' }, { type: 'bar' }]
        }
        myChart.setOption(option)
    }

    // 首屏渲染
    render(arr)


    // 方法二  给每个button注册点击事件，实现相应功能

    // const btns = document.querySelectorAll('[type="button"]')
    // btns.forEach(item => {
    //     item.addEventListener('click',function () {
    //         // console.log(item.innerHTML)
    //            if (document.querySelector('.btn-blue') !== null){
    //                  document.querySelector('button.btn-blue').classList.remove('btn-blue')
    //                  e.target.classList.add('btn-blue')
    // }
    //         arr = []
    //         arr = groupData[item.innerHTML].map(item => Object.values(item))
    //         // console.log(arr)
    //        render(arr)
    //     })
    // })
}

// 双饼状图  男女薪资分布
function initGenderSalaryPieChart(GenderSalaryData) {
    const myChart = echarts.init(document.querySelector('#gender'))

    const b_arr = []
    const g_arr = []
    GenderSalaryData.map(item => b_arr.push({name: item.label , value: item.b_count}))
    GenderSalaryData.map(item => g_arr.push({name: item.label , value: item.g_count}))
    const datas = [b_arr,g_arr];
    const option = {
        title: [
          {text: '男女薪资分布',
          left: 15,
          top:10,
          textStyle: {
            fontWeight: 'normal',
            fontSize: 16
          }
        },
          {
            text: '男生',
            left: '50%',
            top: '45%',
            textAlign: 'center',
            textStyle: {
              fontSize: 12,
            },
          },
          {
            text: '女生',
            left: '50%',
            top: '90%',
            textAlign: 'center',
            textStyle: {
              fontSize: 12,
            },
          },
        ],
        tooltip:{
          trigger: 'item'
        },
        color:['#fda422','#5097ff','#3abcfa','#34d39a'],
          series: datas.map(function (data, idx) {
            var top = idx * 50;
            return {
              type: 'pie',
              radius: [40, 60],
              top: top + '%',
              height: '50%',
              left: 'center',
              width: 300,
              label: {
                minMargin: 5,

                rich: {
                  time: {
                    fontSize: 10,
                    color: '#999'
                  }
                }
              },
              labelLayout: function (params) {
                const points = params.labelLinePoints;
                return {
                  labelLinePoints: points
                };
              },
              data: data
            };
          })
        };
        myChart.setOption(option)
}

// 地图 籍贯分布图
const initMapChart = (provinceData) => {
    const myEchart = echarts.init(document.querySelector('#map'))
    const reg = /省|自治区|维吾尔自治区|回族自治区|壮族自治区/g
    const dataList = [
      { name: '南海诸岛', value: 0 },
      { name: '北京', value: 0 },
      { name: '天津', value: 0 },
      { name: '上海', value: 0 },
      { name: '重庆', value: 0 },
      { name: '河北', value: 0 },
      { name: '河南', value: 0 },
      { name: '云南', value: 0 },
      { name: '辽宁', value: 0 },
      { name: '黑龙江', value: 0 },
      { name: '湖南', value: 0 },
      { name: '安徽', value: 0 },
      { name: '山东', value: 0 },
      { name: '新疆', value: 0 },
      { name: '江苏', value: 0 },
      { name: '浙江', value: 0 },
      { name: '江西', value: 0 },
      { name: '湖北', value: 0 },
      { name: '广西', value: 0 },
      { name: '甘肃', value: 0 },
      { name: '山西', value: 0 },
      { name: '内蒙古', value: 0 },
      { name: '陕西', value: 0 },
      { name: '吉林', value: 0 },
      { name: '福建', value: 0 },
      { name: '贵州', value: 0 },
      { name: '广东', value: 0 },
      { name: '青海', value: 0 },
      { name: '西藏', value: 0 },
      { name: '四川', value: 0 },
      { name: '宁夏', value: 0 },
      { name: '海南', value: 0 },
      { name: '台湾', value: 0 },
      { name: '香港', value: 0 },
      { name: '澳门', value: 0 },
    ]
    dataList.forEach(item => {
        let obj = provinceData.find(v => v.name.replace(reg,'') === item.name)
        if(obj){
            // console.log(item)
            item.value = obj.value
        }
    })
    let option = {
      title: {
        text: '籍贯分布',
        top: 10,
        left: 10,
        textStyle: {
          fontSize: 16,
        },
      },
      tooltip: {
        trigger: 'item',
        formatter: '{b}: {c} 位学员',
        borderColor: 'transparent',
        backgroundColor: 'rgba(0,0,0,0.5)',
        textStyle: {
          color: '#fff',
        },
      },
      visualMap: {
        min: 0,
        max: 6,
        left: 'left',
        bottom: '20',
        text: ['6', '0'],
        inRange: {
          color: ['#ffffff', '#0075F0'],
        },
        show: true,
        left: 40,
      },
      geo: {
        map: 'china',
        roam: false,
        zoom: 1.0,
        label: {
          normal: {
            show: true,
            fontSize: '10',
            color: 'rgba(0,0,0,0.7)',
          },
        },
        itemStyle: {
          normal: {
            borderColor: 'rgba(0, 0, 0, 0.2)',
            color: '#e0ffff',
          },
          emphasis: {
            areaColor: '#34D39A',
            shadowOffsetX: 0,
            shadowOffsetY: 0,
            shadowBlur: 20,
            borderWidth: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
      series: [
        {
          name: '籍贯分布',
          type: 'map',
          geoIndex: 0,
          data: dataList,
        },
      ],
    }
    myEchart.setOption(option)
  }
  
  